// web/src/app/learn/(course)/courses/[courseId]/layout.tsx
import React from 'react';
import {currentUser} from "@/lib/auth";
import {redirect} from "next/navigation";
import {db} from "@/lib/db";
import {getProgress} from "@/actions/learn/get-progress";
import CourseSidebar from "@/app/learn/(course)/courses/[courseId]/_components/course-sidebar";
import CourseNavbar from "@/app/learn/(course)/courses/[courseId]/_components/course-navbar";

const Layout = async ({
                          children,
                          params
                      }: {
    children: React.ReactNode
    params: { courseId: string }
}) => {
    const user = await currentUser()
    if (!user) return redirect('/learn')

    const course = await db.course.findUnique({
        where: {
            id: params.courseId
        },
        include: {
            chapters: {
                where: {
                    isPublished: true
                },
                include: {
                    userProgress: {
                        where: {
                            userId: user.id
                        }
                    }
                },
                orderBy: {
                    position: 'asc'
                }
            },
        },
    })

    if (!course) return redirect('/learn')

    const progressCount = await getProgress(user.id!, course.id)

    return (
        <div className={`h-full`}>
            <div className={`h-[80px] md:pl-80 fixed inset-y-0 w-full z-50`}>
                <CourseNavbar
                    course={course}
                    progressCount={progressCount}
                />
            </div>
            <div className={`hidden md:flex h-full w-80 flex-col fixed inset-y-0 z-50`}>
                {/*@ts-ignore*/}
                <CourseSidebar
                    course={course}
                    progressCount={progressCount}
                />
            </div>
            <main className={`md:pl-80 pt-[80px] h-full`}>
                {children}
            </main>
        </div>
    );
};

export default Layout;